package com.yangtao.springbootfreemarker.web.controller.test;

import cn.hutool.core.util.IdUtil;
import com.yangtao.core.utils.FileUtil;
import com.yangtao.core.utils.MapUtil;
import com.yangtao.core.utils.ParameterUtil;
import com.yangtao.core.utils.StringUtil;
import com.yangtao.core.web.ResultMsg;
import com.yangtao.springbootfreemarker.domain.sys.dao.SysUserDao;
import com.yangtao.springbootfreemarker.web.controller.common.ControllerBase;
import com.yangtao.springbootfreemarker.web.controller.test.utils.Jpg2pdfUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("pub/zy/data")
public class ZyController extends ControllerBase {

	@Autowired
	private SysUserDao sysUserDao;

	String rootPath = "D:/images/001/ucamisweb/";

	/**
	 * 获取单位工程列表数据
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public Object tree() {
		String dataType = ParameterUtil.getParamStr(request, "dataType", "project");
		if(StringUtil.equals(dataType, "project")) {
			return singleProjectList();
		} else if(StringUtil.equals(dataType, "singleProject")) {
			return fileList();
		} else if(StringUtil.equals(dataType, "file")) {
			return recordList();
		}
		return ResultMsg.buildSuccessMsg(null);
	}


	/**
	 * 获取单位工程列表数据
	 * @return
	 */
	@RequestMapping("/singleProjectList")
	@ResponseBody
	public Object singleProjectList() {
		String name = ParameterUtil.getParamStr(request, "name");
		String sql = "select t.name, t.id, 'singleProject' as dataType, 'closed' as state from single_project t where 1=1 ";
		sql += " and t.name like '%"+ name +"%'";
		List<Map<String, Object>> spList = sysUserDao.nQueryListMap(sql);
		return spList;
	}

	/**
	 * 获取案卷列表
	 * @return
	 */
	@RequestMapping("/fileList")
	@ResponseBody
	public Object fileList() {
		String singProjectId = ParameterUtil.getParamStr(request, "singProjectId");
		String sql = "select t.name, t.id, t.order_number, 'file' as dataType, 'closed' as state from file t where t.single_project = '"+ singProjectId +"' order by t.order_number";
		List<Map<String, Object>> list = sysUserDao.nQueryListMap(sql);
		return list;
	}

	/**
	 * 获取案卷列表
	 * @return
	 */
	@RequestMapping("/recordList")
	@ResponseBody
	public Object recordList() {
		String fileId = ParameterUtil.getParamStr(request, "fileId");
		String sql = "select t.name, t.id,t.order_number, 'record' as dataType, 'open' as state from record t where t.file = '"+ fileId +"' order by t.order_number";
		List<Map<String, Object>> list = sysUserDao.nQueryListMap(sql);
		return list;
	}

	/**
	 * 获取案卷列表
	 * @return
	 */
	@RequestMapping("/composePdf")
	@ResponseBody
	public ResultMsg composePdf() {
		String id = ParameterUtil.getParamStr(request, "id");
		String dataType = ParameterUtil.getParamStr(request, "dataType");

		String rSql = "select name,id,file_url as fileUrl from record where 1=1";
		if(StringUtil.equals(dataType, "singleProject")) {
			rSql += " and single_project = '"+ id +"'";
		} else if(StringUtil.equals(dataType, "file")) {
			rSql += " and file = '"+ id +"'";
		} else if(StringUtil.equals(dataType, "record")) {
			rSql += " and id = '"+ id +"'";
		}else {
			rSql += " and 1=2 ";
		}
		List<Map<String, Object>> list = sysUserDao.nQueryListMap(rSql);
		for(Map<String, Object> recordMap : list) {
			System.out.println("开始合成文件名称为【"+ MapUtil.getStr(recordMap, "name") +"】的电子文件");
			String recordId = MapUtil.getStr(recordMap, "id");
			// 验证是否已经合成PDF
			String aiSql = "select * from accessories_info t where t.aii10c = '"+ recordId +"'";
			List<Map<String, Object>> ailist = sysUserDao.nQueryListMap(aiSql);
			if(ailist == null || ailist.size() == 0) {
				jpgTpPdf(recordMap);
			} else {
				System.err.println("文件名称为【"+ MapUtil.getStr(recordMap, "name") +"】的电子文件已经存在PDF,不需要合成");
			}
		}
		return ResultMsg.buildSuccessMsg();
	}

	private void jpgTpPdf(Map<String, Object> recordMap) {
		// 开始合成PDF
		String fileUrl = MapUtil.getStr(recordMap, "fileUrl");
		String pdfPath = FileUtil.appendPath(rootPath, fileUrl);
		String imageFolderPath = "";
		String recordId =MapUtil.getStr(recordMap, "id");
		if(StringUtil.isNotBlank(fileUrl)) {
			String imgUrl = FileUtil.appendPath(rootPath, "Images");
			imgUrl = FileUtil.appendPath(imgUrl, fileUrl.substring(0, fileUrl.lastIndexOf(".")));
			if(FileUtil.exist(imgUrl)) {
				imageFolderPath = imgUrl;
			}
		}
		if(StringUtil.isNotBlank(imageFolderPath)) {
			boolean flag = Jpg2pdfUtil.composePdf(imageFolderPath, pdfPath);
			if(flag) {
				File file = new File(pdfPath);
				String insertSql = "insert into accessories_info values (?, ?, 1, ?, ?, 0, '2020-06-12 14:30:00', 'admin', 'Record', ?, 0, 1, 'admin', '02801', 0, null, null)";
				sysUserDao.nUpdate(insertSql, IdUtil.randomUUID(), file.getName(), file.length(), fileUrl, recordId);
				// 修改状态为已合成
				String updateSql = "update record set scanstatus = 1 where id = ?";
				sysUserDao.nUpdate(updateSql, recordId);
				// 获取路径
				System.out.println("PDF合成成功：文件名称【"+ MapUtil.getStr(recordMap, "name") +"】,ID【"+ recordId +"】的文件：" + pdfPath);
			} else {
				System.err.println("PDF合成失败：文件名称【"+ MapUtil.getStr(recordMap, "name") +"】,ID【"+ recordId +"】的文件扫描件未找到：" + imageFolderPath);
			}
		} else {
			System.out.println("PDF合成失败：文件名称【"+ MapUtil.getStr(recordMap, "name") +"】,ID【"+ recordId +"】的文件扫描件未找到：" + imageFolderPath);
		}
	}

	/**
	 * 生成关联关系
	 * @return
	 */
	@RequestMapping("/generateRelated")
	@ResponseBody
	public Object generateRelated() {
		// 获取单位工程列表
		String singleProjectId = ParameterUtil.getParamStr(request, "singleProjectId");
		String sqSql = "select t.name, t.id from single_project t where 1=1 ";
		if(StringUtil.isNotBlank(singleProjectId)) {
			sqSql += " and id = '"+ singleProjectId +"'";
		}
		List<Map<String, Object>> spList = sysUserDao.nQueryListMap(sqSql);
		String fileUrl;
		String recordName;
		String recordId;
		Integer fileSize;
		for(Map<String, Object> spMap : spList) {
			System.out.println("单位工程开始转换：【"+ MapUtil.getStr(spMap, "name") +"】");
			String spId = MapUtil.getStr(spMap, "id");
			String rSql = "select t.id as recordId, t.name as recordName, t.file_url as fileUrl, t.file_size as fileSize from record t where t.single_project = '"+ spId +"' and file_url is not null";
			// 获取文件列表信息
			List<Map<String, Object>> recordList = sysUserDao.nQueryListMap(rSql);
			for(Map<String, Object> recordMap : recordList) {
				fileUrl = MapUtil.getStr(recordMap, "fileUrl");
				recordId = MapUtil.getStr(recordMap, "recordId");
				recordName = MapUtil.getStr(recordMap, "recordName");
				if(StringUtil.isNotBlank(fileUrl)) {
					// 获取电子文件
					String aiSql = "select t.aii01c from accessories_info t where t.aii10c = ?";
					List<Map<String, Object>> aiList = sysUserDao.nQueryListMap(aiSql, recordId);
					if(aiList == null || aiList.size() == 0) {
						// 验证电子文件是否存在
						String absolutePath = FileUtil.appendPath(rootPath, fileUrl);
						File file = new File(absolutePath);
						if(FileUtil.exist(file)) {
							String insertSql = "insert into accessories_info values (?, ?, 1, ?, ?, 0, '2020-06-12 14:30:00', 'admin', 'Record', ?, 0, 1, 'admin', '02801', 0, null, null)";
							sysUserDao.nUpdate(insertSql, IdUtil.randomUUID(), file.getName(), file.length(), fileUrl, recordId);
							System.out.println("关联成功：文件名称：【"+ recordName +"】, 路径：【"+ fileUrl +"】");
						}else {
							System.out.println("生成失败：文件名称：【"+ recordName +"】, 路径：【"+ fileUrl +"】;无电子文件");
						}
					}
				}
			}
			System.out.println("单位工程结束转换：【"+ MapUtil.getStr(spMap, "name") +"】");
		}
		return ResultMsg.buildSuccessMsg();
	}
}
